<template>
  <div>
   <van-nav-bar
  title="编辑昵称"
  left-text="返回"
  right-text="保存"
  left-arrow
  @click-left="$emit('close')"
  @click-right="onClickRight"
/>
  <van-field
  v-model.trim="message"
  type="textarea"
  placeholder="请输入新的昵称"
  maxlength="7"
  show-word-limit/>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { updateUserProfile } from '@/api/user.js'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },

  data () {
    return {
      message: ''
    }
  },

  created () {
    this.message = this.value
  },

  methods: {
    async   onClickRight () {
      if (this.message === '') return Toast('请编辑昵称')
      try {
        await updateUserProfile({
          name: this.message
        })

        Toast.success('更新成功')
        this.$emit('close')
        this.$emit('input', this.message)
      } catch (err) {
        if (err.response && err.response.status === 409) {
          Toast.fail('昵称已存在')
          return
        }
        Toast.fail('更新失败')
      }
    }
  }
}

</script>

<style scoped lang='less'>

</style>
